import { Model, DataTypes, Optional } from 'sequelize';
import { sequelize } from '../../config/database';

// 用户接口
interface UserAttributes {
  id: string;
  name: string;
  displayId?: string | null;
  shortId?: string | null;
  gender?: number | null;
  avatar?: string | null;
  payGrade?: number | null;
  border?: string | null;
  createdAt?: Date;
  updatedAt?: Date;
}

// 创建时可选的属性
interface UserCreationAttributes extends Optional<UserAttributes, 'createdAt' | 'updatedAt'> {}

// 用户模型类
class User extends Model<UserAttributes, UserCreationAttributes> implements UserAttributes {
  public id!: string;
  public name!: string;
  public displayId!: string | null;
  public shortId!: string | null;
  public gender!: number | null;
  public avatar!: string | null;
  public payGrade!: number | null;
  public border!: string | null;
  
  // 时间戳
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

// 初始化模型
User.init(
  {
    id: {
      type: DataTypes.STRING,
      primaryKey: true,
      allowNull: false
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    displayId: {
      type: DataTypes.STRING,
      allowNull: true
    },
    shortId: {
      type: DataTypes.STRING,
      allowNull: true
    },
    gender: {
      type: DataTypes.INTEGER,
      allowNull: true
    },
    avatar: {
      type: DataTypes.TEXT,
      allowNull: true
    },
    payGrade: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0
    },
    border: {
      type: DataTypes.TEXT,
      allowNull: true
    }
  },
  {
    sequelize,
    tableName: 'users',
    modelName: 'User',
    timestamps: true
  }
);

export default User;